package com.dragon.client.controller.finance;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.finance.IExpensesService;
import com.dragon.repository.domain.dto.ExpensesDTO;
import com.dragon.repository.domain.vo.ExpensesVO;
import com.dragon.repository.entity.Expenses;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-11-24
 */
@RestController
@RequestMapping("expenses")
@Api(tags = "单据类型")
public class ExpensesController {

    @Autowired
    IExpensesService service;

    @GetMapping("list")
    @ApiOperation(value = "单据类型列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "条数", required = true, type = "int"),
            @ApiImplicitParam(name = "type", value = "类型", required = false, type = "int"),
            @ApiImplicitParam(name = "isDisable", value = "禁用状态", required = false, type = "int")
    })
    public ResponseData<List<ExpensesDTO>> expensesList(@RequestParam Integer page,
                                                        @RequestParam Integer limit,
                                                        @RequestParam(required = false) Integer type,
                                                        @RequestParam(required = false) Integer isDisable){
        Page<Expenses> expensesPage = service.expensesList(page, limit, type, isDisable);
        return ResponseData.success(ExpensesDTO.convertList(expensesPage.getRecords()),expensesPage.getTotal());
    }

    @PostMapping("add")
    @ApiOperation(value = "添加")
    public ResponseData expensesAdd(@RequestBody @Validated(Insert.class)ExpensesVO vo){
        service.addToExpenses(ExpensesVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("update")
    @ApiOperation(value = "修改")
    public ResponseData expensesUpdate(@RequestBody @Validated(Update.class)ExpensesVO vo){
        service.updateExpenses(ExpensesVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("disableEnable")
    @ApiOperation(value = "禁用启用")
    public ResponseData disableEnable(
            @ApiParam(name = "id", value = "id", required = true, type = "int")
            @RequestParam Integer id){
        service.disableEnable(id);
        return ResponseData.success();
    }


}
